/**
 * 
 */
package evance.server.common.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections15.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.stereotype.Service;

import evance.data.model.entity.EvRole;
import evance.server.common.dao.RoleDAO;

/**
 * @author BaoVuong
 *
 */
@Service
public class SecurityRoleServiceImpl implements SecurityRoleService {
	public static final String ROLE_DEFAULT = "NO_ROLE";

	@Autowired
	private RoleDAO roleDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * evance.server.common.service.SecurityRoleService#getRoles(java.lang.Long)
	 */
	@Override
	public Collection<GrantedAuthority> getRoles(Long customerId) {
		Map<String, GrantedAuthority> map = new HashedMap<String, GrantedAuthority>();
		List<EvRole> roles = roleDao.getListRoles(customerId);
		for (EvRole evRole : roles) {
			map.put(evRole.getCode(), new GrantedAuthorityImpl(evRole.getCode()));
		}
		if (map.size() == 0) {
			map.put(ROLE_DEFAULT, new GrantedAuthorityImpl(ROLE_DEFAULT));
		}
		return map.values();
	}
}
